$(function () {
    //页面加载完成获取第一页数据
    getRoleList(1);
    $("body").on('click','#btn1,#btn2',function () {
        //点击搜索查询从第一页开始查询
        getRoleList(1);
    }).on("click",".fa-unlock-alt",function(){ //冻结角色
        var obj = $(this).parent('a');
        var roleId = obj.data("id");
        updateSysRoleStatus(2 , roleId);
    }).on("click",".fa-history",function(){//恢复角色
        var obj = $(this).parent('a');
        var roleId = obj.data("id");
        updateSysRoleStatus(1 , roleId);
    }).on("click","#lockBatch",function () {//批量冻结
        var obj = $("input[name='roleIds']:checked");
        var roleIds = obtainIds(obj);//获取勾选ids
        if(!$.isEmptyObject(roleIds)){
            updateSysRoleStatus(2, roleIds);
        }
    }).on("click","#regainBatch",function () {//批量恢复
        var obj = $("input[name='roleIds']:checked");
        var roleIds = obtainIds(obj);//获取勾选ids
        if(!$.isEmptyObject(roleIds)){
            updateSysRoleStatus(1, roleIds);
        }
    }).on("click",".role-delete-btn",function(){ //单个删除
        var roleId = $(this).data('id');
        deleteBatch(roleId);
    }).on("click","#deleteBatch",function () {//批量删除
        var obj = $("input[name='roleIds']:checked");
        var roleIds = obtainIds(obj);//获取勾选ids
        if(roleIds!=null && $.trim(roleIds)!=''){
            deleteBatch(roleIds);//批量删除
        }
    }).on("click",".role-check-btn",function () {//查看角色详情
        var roleId = $(this).data('id');
        var roleName = $("#role-name-"+roleId).text();
        var index = parent.layer.open({
            type: 2,
            shadeClose: false,
            shade: [0.3, '#000000'],
            title: '<strong>查看角色【'+ roleName +'】</strong>',
            area: ['800px', '500px'],
            content: baseloaction + "/admin/manage/role/roleInfo?id="+roleId,
            btn: ['进入修改状态', '取消'],
            yes: function(index, layero) {
                window.location.href= baseloaction + '/admin/manage/role/initUpdateRole?roleId='+roleId;
                parent.layer.close(index);
            }
        });
    });

    /**
     * 获取角色列表页面
     * @param pageNo 当前页码
     */
    function getRoleList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/manage/role/ajax/ajaxListSysRole',//请求URL
            contextId:'role-list-context',//响应内容页面设置内容ID
            params:params,//请求参数
            pageNo:pageNo,//页码
            pageCall:getRoleList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId:'select-all-role', //全选框ID
            callback:function (pageNo) {//数据加载完成回调
            }
        };
        ajaxGetListData(obj)
    }

    //修改状态
    function updateSysRoleStatus(status, roleIds) {
        $.ajax({
            url: baseloaction + "/admin/manage/role/updateSysRoleStatus",
            data:{"roleIds":roleIds,"status":status},
            type:"post",
            dataType:"json",
            success:function (result) {
                if(result.success){
                    var arr = (roleIds+"").split(",");
                    $.each(arr,function (index,em) {
                        if(em!=null && $.trim(em)!=''){
                            var statusEm = $("#role-status-"+em);//状态元素
                            var actionEm = $(".role-action-status-"+em);//操作的元素
                            if(status==1){//正常
                                statusEm.html('<span class="label label-info">正常</span>');
                                actionEm.html('<i class="fa fa-unlock-alt"></i>');
                            }
                            if(status==2){//冻结
                                statusEm.html('<span class="label label-danger">冻结</span>');
                                actionEm.html('<i class="fa fa-history"></i>');
                            }
                        }
                    });
                }else{
                    icModale(0,result.message);
                }
            },
            error:function (error) {
                icModale(0,"系统繁忙,请稍后重试");
            }
        })
    }

    //判断勾选ID是否为空
    function obtainIds(obj) {
        if(obj.size() == 0){
            icModale(0,"请选择要操作的角色记录");
            return null;
        }else {
            var roleIds = "";
            obj.each(function (index, em) {
                roleIds += em.value + ",";
            });
            roleIds = roleIds.substr(0,roleIds.length-1);
            return roleIds;
        }
    }

    //删除角色
    function deleteBatch(roleIds) {
        icModale(2,"确定要删除选中角色吗?",3,function (){
            $.ajax({
                url: baseloaction + "/admin/manage/role/deleteSysRoleByIds",
                data:{"ids":roleIds},
                type:"post",
                dataType:"json",
                success:function (result) {
                    if(result.success){
                        var arr = (roleIds+"").split(",");
                        var _index = 0;
                        $.each(arr,function (index,em) {
                            if(em!=null && $.trim(em)!=''){
                                $("#role-data-"+em).remove();
                                _index ++;
                            }
                        });
                    }else{
                        icModale(0,result.message);
                    }
                },
                error:function (error) {
                    icModale(0,"系统繁忙,请稍后重试");
                }
            });
        });
    }
});
